package com.example.contactapp.adapters;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.SumPathEffect;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.contactapp.R;
import com.example.contactapp.SmsDetailActivity;
import com.example.contactapp.objects.SmsObject;
import com.example.contactapp.objects.ThreatObject;

@SuppressLint("SimpleDateFormat")
public class SmsAdapter extends BaseAdapter{
	public static ArrayList<SmsObject> curSMSES;
	private ArrayList<Integer> myArrayChecked;
	private Context myContext;
	private HashMap<String, ArrayList<SmsObject>> mAllSmses;
	private ArrayList<String> objects;

	public SmsAdapter(Context context, HashMap<String, ArrayList<SmsObject>> _mAllSmses) {
		myArrayChecked = new ArrayList<>();
		myContext = context;
		this.mAllSmses = _mAllSmses;
		objects = new ArrayList<>(mAllSmses.keySet());
		Collections.sort(objects, new Comparator<String>() {

			@Override
			public int compare(String lhs, String rhs) {
				ArrayList<SmsObject> leftArray = mAllSmses.get(lhs);
				ArrayList<SmsObject> rightArray = mAllSmses.get(rhs);
				long leftTime = leftArray.get(leftArray.size()-1).getTime();
				long rightTime = rightArray.get(rightArray.size()-1).getTime();
				
				if(leftTime > rightTime) return -1;
				else if(leftTime < rightTime) return 1;
				else return 0;
			}
		});
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(myContext).inflate(
					R.layout.sms_item, null);
			holder = new ViewHolder();
			holder.tvNumber = (TextView) convertView
					.findViewById(R.id.tv_number);
			holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tvHours = (TextView) convertView.findViewById(R.id.tv_hours);
			holder.tvContent = (TextView) convertView
					.findViewById(R.id.tv_content);
			holder.imgAvatar = (ImageView) convertView.findViewById(R.id.img_avatar);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final String threat = getItem(position);
		int count = mAllSmses.get(threat).size();
		final SmsObject sms = mAllSmses.get(threat).get(count-1);
		holder.tvHours.setText(sms.getStrTime());
		holder.tvContent.setText(sms.getBodySms());
		holder.tvName.setText(sms.getName());
		
		if(count > 1){
			holder.tvNumber.setVisibility(View.VISIBLE);
			holder.tvNumber.setText("("+count+")");
		}else{
			holder.tvNumber.setVisibility(View.INVISIBLE);
		}
		if(null == sms.getAvatar() || sms.getAvatar().length() == 0){
			holder.imgAvatar.setImageDrawable(myContext.getResources().getDrawable(R.drawable.ic_contact_picture));
        }else{
        	holder.imgAvatar.setImageURI(Uri.parse(sms.getAvatar()));
        }
		
		convertView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(myContext, SmsDetailActivity.class);
				i.putExtra("ADDRESS", sms.getAddress());
				curSMSES = mAllSmses.get(threat);
				myContext.startActivity(i);
			}
		});
		return convertView;
	}

	public ArrayList<Integer> getArrayChecks() {
		return myArrayChecked;
	}

	private static class ViewHolder {
		TextView tvName;
		TextView tvNumber;
		TextView tvHours;
		TextView tvContent;
		ImageView imgAvatar;
	}


	

	public int getCountChecked() {
		return myArrayChecked.size();
	}

	public void clearChecked() {
		myArrayChecked.clear();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return objects.size();
	}

	@Override
	public String getItem(int arg0) {
		// TODO Auto-generated method stub
		return objects.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	
}
